<?php
class CAdminUser
{
	static function get($p_intPage, $p_intItemOnPerPage, $p_intId=null, $p_strKeyword=null, $p_intGroupId=null, $p_intGroupLeader=2, $p_strCreateUser=null)
	{
		$AppUI = Zend_Registry::get('AppUI');
		$arrResult = array();
		if ($p_intId)
		{
			$arrResult = QAdminUser::getById($p_intId);
		}
		else
		{
			$arrResult = QAdminUser::get($p_intPage, $p_intItemOnPerPage, $AppUI->admin_id, $p_strKeyword, $p_intGroupId, $p_intGroupLeader, $p_strCreateUser);
		}
		$intTotal = is_array($arrResult) && $arrResult ? count($arrResult) : 0;
		for($i=0; $i<$intTotal; $i++)
		{
			$arrResult[$i]['id'] = isset($arrResult[$i]['admin_id']) ? intval($arrResult[$i]['admin_id']) : null;
			$arrResult[$i]['username'] = isset($arrResult[$i]['admin_username']) ? Common_FCommon::deCode($arrResult[$i]['admin_username']) : null;
			$arrResult[$i]['createdate'] = isset($arrResult[$i]['admin_createdate']) ? Common_FCommon::deCode($arrResult[$i]['admin_createdate']) : null;
			$arrResult[$i]['lastlogin'] = isset($arrResult[$i]['admin_lastlogin']) ? Common_FCommon::deCode($arrResult[$i]['admin_lastlogin']) : null;
			$arrResult[$i]['createuser'] = isset($arrResult[$i]['admin_createuser']) ? Common_FCommon::deCode($arrResult[$i]['admin_createuser']) : null;
			$arrResult[$i]['firstname'] = isset($arrResult[$i]['admin_firstname']) ? Common_FCommon::deCode($arrResult[$i]['admin_firstname']) : null;
			$arrResult[$i]['lastname'] = isset($arrResult[$i]['admin_lastname']) ? Common_FCommon::deCode($arrResult[$i]['admin_lastname']) : null;
			$arrResult[$i]['deparment'] = isset($arrResult[$i]['admin_deparment']) ? Common_FCommon::deCode($arrResult[$i]['admin_deparment']) : null;
			$arrResult[$i]['position'] = isset($arrResult[$i]['admin_position']) ? Common_FCommon::deCode($arrResult[$i]['admin_position']) : null;
			$arrResult[$i]['group_id'] = isset($arrResult[$i]['group_id']) ? Common_FCommon::deCode($arrResult[$i]['group_id']) : null;
			$arrResult[$i]['group_name'] = isset($arrResult[$i]['group_name']) ? Common_FCommon::deCode($arrResult[$i]['group_name']) : null;
			$arrResult[$i]['isactive'] = isset($arrResult[$i]['admin_isactive']) ? intval($arrResult[$i]['admin_isactive']) : null;
			$arrResult[$i]['group_leader'] = isset($arrResult[$i]['group_leader']) ? intval($arrResult[$i]['group_leader']) : null;
		}
		return $arrResult;
	}
	
	static function getTotal($p_strKeyword=null, $p_intGroupId=null, $p_intGroupLeader=2, $p_strCreateUser=null)
	{
		$AppUI = Zend_Registry::get('AppUI');
		$arrResult = QAdminUser::getTotal($AppUI->admin_id, $p_strKeyword, $p_intGroupId, $p_intGroupLeader, $p_strCreateUser);
		return intval(isset($arrResult[0]['total']) ? $arrResult[0]['total'] : 0);
	}
	
	static function insert($p_strName, $p_strPassword, $p_strFirstname, $p_strDepartment, $p_strPosition, $p_intGroupId, $p_strGroupName, $p_intGroupLeader)
	{
		$AppUI = Zend_Registry::get('AppUI');
		if ($AppUI->admin_id)
		{
			$strName = Common_FCommon::enCode($p_strName, ADMIN_USER_USERNAME_LIMIT);
			$strPassword = md5($p_strPassword);
			$strCreateUser = Common_FCommon::enCode($AppUI->admin_username);
			$strFirstname = Common_FCommon::enCode($p_strFirstname, ADMIN_USER_FULLNAME_LIMIT);
			$strDepartment = Common_FCommon::enCode($p_strDepartment, ADMIN_USER_DEPARMENT_LIMIT);
			$strPosition = Common_FCommon::enCode($p_strPosition, ADMIN_USER_POSITION_LIMIT);
			$intGroupId = intval($p_intGroupId);
			$strGroupName = Common_FCommon::enCode($p_strGroupName, ADMIN_USER_GROUPNAME_LIMIT);
			$arrResult = QAdminUser::insert($strName, $strPassword, $strCreateUser, $strFirstname, $strDepartment, $strPosition, $intGroupId, $strGroupName, $p_intGroupLeader);
			$intLastId = isset($arrResult[0]['last_id']) ? $arrResult[0]['last_id'] : null;
			if ($intLastId)
			{
				$strLog = 'Username='.$strName.'|Firstname='.$strFirstname.'|Deparment='.$strDepartment.'|Position='.$strPosition.'|GroupId='.$intGroupId.'|GroupName='.$strGroupName.'|Leader='.$p_intGroupLeader;
				CAdminLog::insert(ADMIN_USER_ADD, $strLog);
			}
			return $intLastId;
		}
		return 0;
	}
	
	static function update($p_intId, $p_strFirstname, $p_strDepartment, $p_strPosition, $p_intGroupId, $p_strGroupName, $p_intGroupLeader=0)
	{
		$bolResult = true;
		$intId = intval($p_intId);
		if ($intId)
		{
			$strFirstname = Common_FCommon::enCode($p_strFirstname, ADMIN_USER_FULLNAME_LIMIT);
			$strDepartment = Common_FCommon::enCode($p_strDepartment, ADMIN_USER_DEPARMENT_LIMIT);
			$strPosition = Common_FCommon::enCode($p_strPosition, ADMIN_USER_POSITION_LIMIT);
			$intGroupId = intval($p_intGroupId);
			$strGroupName = Common_FCommon::enCode($p_strGroupName, ADMIN_USER_GROUPNAME_LIMIT);
			QAdminUser::update($intId, $strFirstname, $strDepartment, $strPosition, $intGroupId, $strGroupName, $p_intGroupLeader);
			if ($bolResult)
			{
				$strLog = '|Firstname='.$strFirstname.'|Deparment='.$strDepartment.'|Position='.$strPosition.'|GroupId='.$intGroupId.'|GroupName='.$strGroupName.'|Leader='.$p_intGroupLeader;
				CAdminLog::insert(ADMIN_USER_EDIT, $strLog);
			}
		}
		return $bolResult;
	}
	
	static function updatePassword($p_strPassOld, $p_strPassNew)
	{
		$AppUI = Zend_Registry::get('AppUI');
		$strPassOld = md5($p_strPassOld);
		$strPassNew = md5($p_strPassNew);
		$bolResult = QAdminUser::updatePassword($AppUI->admin_username, $strPassOld, $strPassNew);
		if ($bolResult)
		{
			#$strLog = 'Username='.$AppUI->admin_username.'|Pwd-Old:'.$p_strPassOld;
			#CAdminLog::insert(ADMIN_USER_CHANGE_PWD, $strLog);
		}
		return $bolResult;
	}
	
	static function updateActive($p_strId, $p_intActive)
	{
		$bolResult = false;
		$bolResult = QAdminUser::updateActive($p_strId, $p_intActive);
		if ($bolResult)
		{
			$strLog = 'Log_id='.$p_strId.'|Active='.$p_intActive;
			CAdminLog::insert(ADMIN_USER_ACTIVE, $strLog);
		}
		return $bolResult;
	}
	
	static function getListAdminLeader()
	{
		$arrData = QAdminUser::get(1,1000,ADMINISTRATOR,null,null,1);
		$intTotal = is_array($arrData) && $arrData ? count($arrData) : 0;
		$arrResult = array();
		for($i=0; $i<$intTotal; $i++)
		{
			$arrResult[$i] = $arrData[$i]["admin_id"];
		}
		return $arrResult;
	}
	
	static function checkExistUsername($p_strUsername)
	{
		$p_strUsername = Common_FCommon::enCode($p_strUsername);
		$arrResult = QAdminUser::checkExistUsername($p_strUsername) ;
		return (isset($arrResult[0]['admin_id']) && $arrResult[0]['admin_id']) ? true : false ;
	}
}
?>